/****************************************************************************
 * @file    : lin_config.h
 * @author  : Nonosns MCU Team
 * @version : V2.0
 * @Date    : 2023/4/11
 * @brief   : lin stack file
 * @note
 * Copyright (C) 2022 Novosense All rights reserved.
 ****************************************************************************/
#ifndef  LIN_CONFIG_H
#define  LIN_CONFIG_H

/*******************************************************************************
* Include files
*******************************************************************************/
#include "lin_api.h"
#include "lin_define.h"

/*******************************************************************************
* definitions
*******************************************************************************/
#define INITIAL_NAD            0x01
#ifdef L_LIN_2_2
  #define FUNCTIONAL_NAD       0x7E
#endif  
#define WILDCARD_NAD           0x7F
#define WILDCARD_SUPPLIER_ID   0x7FFF
#define WILDCARD_FUNCTION_ID   0xFFFF

#ifdef L_NAD_EEPROM  
    #define EEPROM_NAD_PAGE             10            // offset to EEPROM page 10 where the NAD is stored (NAD is stored to byte 0)
    #define NAD_VALID_PATTERN          0xA55Au        // pattern on byte 2 and 3 of EEPROM_NAD_ADDRESS, that indicates the NAD as valid
#endif

#ifdef L_PID_EEPROM  
    #define EEPROM_PID_PAGE             11            // offset to EEPROM page 11 where the PID is stored (PID is stored to byte 2)
#endif
#ifdef L_UDS_BOOTLOADER_UPDATE
    #define BOOT_INFO_EEPROM_PAGE      60            // offset to EEPROM page 60 where the BootInfo is stored
#endif


#define RESPONSE_ERROR_FRAME   0       // number of frame (in g_nodeConfigFrameTable[]) containing the Response_Error signal
#define RESPONSE_ERROR_BYTE    7       // number of byte carrying the Response_Error signal
#define RESPONSE_ERROR_BIT    BIT_0    // bit carrying the Response_Error signal

#define MOTOR_RESPONSE_FRAME   5  // number of frame (in g_nodeConfigFrameTable[]) containing Motor angle and falut signal
#define MOTOR_REQUEST_FRAME    4  // number of frame (in g_nodeConfigFrameTable[]) containing Request angle and pwm duty signal

#define LED_RESPONSE_FRAME   2    // number of frame (in g_nodeConfigFrameTable[]) containing the led_state signal
#define KEY_REQUEST_FRAME    3    // number of frame (in g_nodeConfigFrameTable[]) containing the key_state signal

//-----------------------------
// Interrupt priorities
//-----------------------------
#define LINUART_IRQ_PRIO      0          // set priority for LINUART IRQ to 0 (default after reset) 
#define LIN_IRQ_PRIO          0          // set priority for LIN I/O IRQ to 0 (default after reset)


/*******************************************************************************
*  type definitions
*******************************************************************************/

#ifdef L_UDS_BOOTLOADER_UPDATE

typedef struct {
    uint8_t infoDataLen;            /*Exchange inforamtion length must N * 4.*/
    uint8_t requestEnterBootloader; /*Request enter bootloader mode flag*/
    uint8_t downloadAPPSuccessful;  /*downlaod APP successful flag*/
    uint32_t infoStartAddr;         /*exchange information start address*/
    uint32_t requestEnterBootloaderAddr; /*Request enter bootloader flag address */
    uint32_t downloadAppSuccessfulAddr; /*download APP successful flag address*/
} BootInfo;

#endif
// IDs for node configuration
typedef struct {
    l_u16 supplierID;
    l_u16 functionID;
    l_u8 variantID;
} ProductIdentifier;

#define L_SERIAL_NUMBER        0x12345678
#define L_SUPPLIER_ID          0x0041    /* 0x0041 Just For example, Novosns don't supply Supplier ID */
#define L_FUNCTION_ID          0x1234
#define L_VARIANT_ID           0xAB

#define L_ID_TABLE_SIZE         8    // number of supported IDs (including 0x3C and 0x3D)

#define DIAGNOSIS_REQUEST_FRAME  L_ID_TABLE_SIZE - 2
#define DIAGNOSIS_RESPONSE_FRAME L_ID_TABLE_SIZE - 1



/*******************************************************************************
*  externals
*******************************************************************************/
extern const l_u8 g_serialNumber[];
extern const ProductIdentifier g_productIdentifier;
#if !(defined L_LIN_2_2)
extern const l_u16 g_configMessageIDTable[];
#endif
extern l_u8 g_nodeFramePIDTable[];
extern const ConfigFrame g_nodeConfigFrameTable[];


#endif   /* LIN_CONFIG_H  */
